<template>
  <div :id="id" :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    className: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: 'pie_chart'
    },
    width: {
      type: String,
      default: '400px'
    },
    height: {
      type: String,
      default: 'calc(100vh/4)'
    }
  },
  data () {
    return {
      pie_chart: null
    }
  },
  mounted () {
    this.initPieChart()
  },
  beforeDestroy () {
    if (!this.pie_chart) {
      return
    }
    this.pie_chart.dispose()
    this.pie_chart = null
  },
  methods: {
    initPieChart () {
      this.pie_chart = echarts.init(document.getElementById(this.id))
      this.setOption(null)
    },
    setOption (data) {
      this.pie_chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          data: data == null ? ['ALL'] : data.legendData
        },
        color: [
          '#c23531',
          '#2f4554',
          '#61a0a8',
          '#d48265',
          '#91c7ae',
          '#749f83',
          '#ca8622',
          '#bda29a',
          '#6e7074',
          '#546570',
          '#c4ccd3'
        ],
        series: [
          {
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            data: data == null ? [{ value: 0, name: 'ALL' }] : data.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }, true)
    }
  }
}
</script>
